package com.hqq.thread.tls;

import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * DateFormatCallableTestMultiThread
 * Created by heqianqian on 2017/7/2.
 */
public class DateFormatCallableTestMultiThread {

    static Result[] result = new Result[4];

    static class StringArrayList extends ArrayList<String> {
    /* nothing needed here */
    }

    static List<String>[] createdDateValues = new StringArrayList[4];

    int expectedCounterDateValues = 5;

    int expectedCounterExceptions = 0;

    List<String> expectedDateValues = Arrays.asList("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");


    @Before
    public void setUp() throws Exception {
        // Create a callable
        DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
        // start thread using the Callable instance
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Result> futureResult1 = executor.submit(callableDf);
        Future<Result> futureResult2 = executor.submit(callableDf);
        Future<Result> futureResult3 = executor.submit(callableDf);
        Future<Result> futureResult4 = executor.submit(callableDf);
        try {
            result[0] = futureResult1.get();
            result[1] = futureResult2.get();
            result[2] = futureResult3.get();
            result[3] = futureResult4.get();
            for (int i = 0; i < result.length; i++) {
                createdDateValues[i] = convertDatesToString(result[i]);
            }
        } catch (Exception e) {
            fail("Setup failed: " + e);
        }
        executor.shutdown();
    }

    private List<String> convertDatesToString(Result result) {
        // Format date value as DD.MM.YYYY
        if (result == null || result.getDateList() == null || result.getDateList().size() == 0) {
            return null;
        }
        List<String> returnList = new StringArrayList();

        for (Date dt : result.getDateList()) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
        }
        return returnList;
    }

    @Test
    public void testDateValues() throws Exception {
        for (int i = 0; i < createdDateValues.length; i++) {
            assertEquals(expectedDateValues, createdDateValues[i]);
        }
    }

    @Test
    public void testCounterDateValues() {
        for (int i = 0; i < result.length; i++) {
            assertEquals(expectedCounterDateValues, result[i].getDateList().size());
        }
    }

    @Test
    public void testCounterExceptions() {
        for (int i = 0; i < result.length; i++) {
            assertEquals(expectedCounterExceptions, result[i].getExceptionList().size());
        }
    }
}
